package com.java.digitalsignagesystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.java.digitalsignagesystem.entity.Indicator;
import com.java.digitalsignagesystem.mapper.IndicatorMapper;
import com.java.digitalsignagesystem.service.IndicatorService;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * (Indicator)表服务实现类
 *
 * @author makejava
 * @since 2023-08-25 09:49:44
 */
@Service("indicatorService")
public class IndicatorServiceImpl extends ServiceImpl<IndicatorMapper, Indicator> implements IndicatorService {

    @Value("${vpc.ip}")
    private String vpcIp;

    @Value("${vpc.port}")
    private String vpcPort;

    @Resource
    private IndicatorMapper indicatorMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public Indicator queryById(Integer id) {
        return this.indicatorMapper.queryById(id);
    }


    /**
     * 新增数据
     *
     * @param indicator 实例对象
     * @return 实例对象
     */
    @Override
    public Indicator insert(Indicator indicator) {
        this.indicatorMapper.insert(indicator);
        return indicator;
    }

    /**
     * 修改数据
     *
     * @param indicator 实例对象
     * @return 实例对象
     */
    @Override
    public Indicator update(Indicator indicator) {
        this.indicatorMapper.update(indicator);
        return this.queryById(indicator.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.indicatorMapper.deleteById(id) > 0;
    }

    @Override
    public List<Indicator> getByUserId(String id) {
        List data=new ArrayList<>();
        List<Indicator> indicators = indicatorMapper.selectTheme(id);
        for(int i=0;i<indicators.size();i++){
            LambdaQueryWrapper<Indicator> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Indicator::getTheme,indicators.get(i).getTheme());
            List<Indicator> list = indicatorMapper.selectList(queryWrapper);
            for (int j=0;j<list.size();j++){
                list.get(j).setJson(this.getData(list.get(j).getCode()));
            }
            data.add(list);
        }
        return data;
    }

    @Override
    public String getData(String code)  {
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url("http://"+vpcIp+":"+vpcPort+"/service/api/zp/flcz?code=" + code)
                .get()
                .addHeader("content-type", "application/json")
                .build();

        try (Response response = client.newCall(request).execute()) {
            return response.body().string();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void deleteByTheme(String theme) {
        LambdaQueryWrapper<Indicator> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Indicator::getTheme,theme);
        indicatorMapper.delete(queryWrapper);
    }
}
